using Needle.Framework;
using Needle.Utils;

namespace Needle.Core.Activation
{
    /// <summary>
    /// An abstract base class to simplify activator construction.
    /// </summary>
    public abstract class ActivatorBase : Component, IActivator
    {

        #region constructor

        /// <summary>
        /// intializes the activator base.
        /// </summary>
        public ActivatorBase(InstanceId id, IActivationInfo info)
        {
            Ensure.NotNull(id, "id");
            Ensure.NotNull(info, "info");
            m_Id = id;
            _Info = info;
        }

        #endregion

        #region IActivator Members

        private InstanceId m_Id;
        /// <summary>
        /// The id for this activator.
        /// </summary>
        public InstanceId Id
        {
            get { return m_Id; }
        }

        private IActivationInfo _Info;
        /// <summary>
        /// The concrete type that will be activated.
        /// </summary>
        public IActivationInfo Info
        {
            get { return _Info; }
        }

        /// <summary>
        /// To be implemented by derived classes.
        /// </summary>
        public abstract void ConfigureInstance(object instance);

        /// <summary>
        /// To be implemented by derived classes.
        /// </summary>
        public abstract object CreateInstance();

        #endregion

        #region Protected Members

        private IActivationManager _Manager;
        /// <summary>
        /// gets the ActivationManager.
        /// </summary>
        protected IActivationManager Manager
        {
            get
            {
                if (_Manager == null)
                    _Manager = GetService<IActivationManager>();
                return _Manager;
            }
        }

        #endregion

        /// <summary></summary>
        public override System.ComponentModel.ISite Site
        {
            get { return base.Site; }
            set
            {
                base.Site = value;
                _Manager = null;
            }
        }

    }
}
